package com.isnmp.common.snmp.impl;

import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.Target;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.Variable;
import org.snmp4j.smi.VariableBinding;

import com.isnmp.common.base.MyException;
import com.isnmp.common.snmp.SNMPAPI;

public class GetOIDValueAction {

    /**
     * 
     * @param oid
     * @param snmp
     * @param target
     * @return
     * @throws MyException
     */
    public static Variable getValue(String oid, Snmp snmp, Target target) throws MyException {
        Object localObject;
        try {
            PDU pdu1 = new PDU();
            pdu1.add(new VariableBinding(new OID(oid)));

            //            localObject = snmp.get(pdu1, paramTarget);
            localObject = snmp.send(pdu1, target);

            SNMPUtils.checkSNMPErro((ResponseEvent) localObject);
            PDU localPDU2 = ((ResponseEvent) localObject).getResponse();
            VariableBinding localVariableBinding = localPDU2.get(0);
            Variable localVariable = localVariableBinding.getVariable();
            String str = localVariable.toString();
            if ((str.equalsIgnoreCase("noSuchInstance")) || (str.equalsIgnoreCase("No such name")) || (str.equalsIgnoreCase("noSuchObject")))
                throw new MyException(SNMPAPI.MessageNoSuchOID);
            return localVariable;
        } catch (Exception e) {
            // e.printStackTrace();
            localObject = Messages.getString("GetOIDValueAction.3") + oid + Messages.getString("GetOIDValueAction.4") + e.getMessage();
        }
        throw new MyException((String) localObject);
    }

    public static void main(String[] paramArrayOfString) {
        new GetOIDValueAction();
    }
}